//注意：该段代码移植性可能不高。本人使用 C++11 标准书写代码，可能不支持老式实现；
//其次，本人在代码行内携使用了 DOS （即 Windows CMD）指令，不清楚是否能应用于其他系统中。
#include <iostream>
#include <fstream>
#include <ctime>

//元素数量常量，本人使用其中部分常量作为判断条件，您可以通过更改常量以改变程序运行次数（0.0.3 for Release）
const int c_Size {20};
const int i_Size {5};

//结构 & 共用体 & 指针 尝试
const char* c_Message[] =
{
    "键入一个整数以选择您需要执行的程序。\n",
    "1 - 获取指定数值并相加。\t",
    "2 - 获取指定行数字符并拼接。\t",
    "3 - 使用函数返回结构指针。\t",
    "任意整数 - 计时并退出。\n",
    "键入一行字符",
    "以选择您需要执行的程序",
    "结果输出：",
    "返回的指针 - "
};
struct str_num
{
    int i_num;
    double d_num;
};

struct un_Str
{
    union
    {
        double d_Un;
        char c_Un[c_Size];
    };
};
//枚举测试
enum en_Test{Yes = 1,No = 0,Null = 2};

//获取指定数值并相加
int* for_Test(int);
//获取指定行数字符并拼接
std::string* str_Test(int);
//使用函数返回结构指针
str_num f_str(str_num*);
//安全输入数字
int safeCin(void);
//简单的计时器
void timer(int);

int main()
{
    //修改输出编码
    //此为 DOS 指令，如需移植至其他平台清移除。
    system("chcp 65001>nul");

    //预编译指令
    using std::cin;
    using std::cout;
    using std::endl;

    int i_Choice;
    int temp;
    cout << c_Message[0] << c_Message[2] << "。" << endl
         << "1 - 获取指定数值并相加。\t"
         << "2 - 获取指定行数字符并拼接。\t"
         << "3 - 使用函数返回结构指针。\t"
         << "任意整数 - 计时并退出。\n";
    i_Choice = safeCin();
    if (i_Choice == 1)
        {
        cout << c_Message[0] << c_Message[2] << "的次数。" << endl;
        temp = safeCin();
        int* i_Number = for_Test(temp);
        cout << c_Message[4] << *i_Number << endl;
        delete i_Number;
        }
    else if (i_Choice == 2)
        {
        cout << c_Message[0] << c_Message[2] << "的次数。" << endl;
        temp = safeCin();
        std::string* s_Word = str_Test(temp);
        cout << c_Message[4] << *s_Word << endl;
        delete s_Word;
        }
    else if (i_Choice == 3)
        {
        cout << c_Message[0] << "以计算。" << endl;
        str_num s_num;
        s_num.i_num = safeCin();
        s_num.d_num = safeCin();
        f_str(&s_num);
        cout << c_Message[4] << s_num.d_num << std::endl;
        }
    else
        {
        cout << c_Message[0] << "以计时。" << endl;
        temp = safeCin();
        timer(temp);
        };

    //使程序运行完毕后暂存结果
    //此为 DOS 指令，如需移植至其他平台清移除。
    system("pause");
    system("exit");
    return 0;
};

int* for_Test(int Size)
{
    //预编译设置
    using std::cin;
    using std::cout;
    using std::endl;

    //利用 new 分配临时内存
    un_Str str_Test;
    int i_Test;
    int* i_TestAll {new int[Size]};
    int* i_CoutAll {new int};
    *i_CoutAll = 0;
    //输入数字
    cout << c_Message[0] << "以计算。\t( 0 / " << (Size + 1) << " )" << endl;
    i_Test = safeCin();
    //使用 while 循环执行任意五个 double 数与 *d_Test 的加法
    for (int times = 0; times < Size ; times++)
        {
            //输入五个数值
            cout << "再次" << c_Message[0] << "以计算。\t( " << (times + 1) << " / " << (Size + 1) << " )" << endl;
            str_Test.d_Un = safeCin();
            //输出五个结果
            cout << i_Test << " + " << str_Test.d_Un << " = "
                 << (i_TestAll[times] = i_Test + str_Test.d_Un) << endl;
            *i_CoutAll += i_TestAll[times];
        }
    cout << c_Message[3] << endl;
    //使用 for 输出上方所有算式结果
    for (int times = 0; times < (Size - 1) ; times++)
        {
            cout << i_TestAll[times] << " + " ;
        }
    cout << i_TestAll[Size - 1] << " = "
         << *i_CoutAll << endl;
    //返回值
    delete [] i_TestAll;
    return i_CoutAll;
};
std::string* str_Test(int times)
{
    using std::string;
    //分配内存
    string str_Test[times];
    string* str_TestAll {new string};
    std::cin.get();
    //输入字符
    for (string s_char : str_Test)
    {
        std::cout << c_Message[1] << "。" << std::endl;
        getline(std::cin,s_char);
        *str_TestAll += s_char;
    };
    //输出结果
    std::cout << "你输入了 " << (*str_TestAll).size() << " 个字。" << std::endl
              << c_Message[3] << *str_TestAll << std::endl;

    return str_TestAll;
};

str_num f_str(str_num* p_num)
{
    p_num->i_num < p_num->d_num ? p_num->d_num += p_num->i_num : p_num->d_num *= p_num->i_num;
    return *p_num;
};

int safeCin(void)
{
    using std::cin;
    int i_temp;
    char c_temp;
    while (!(cin >> i_temp))
    {
        cin.clear();
        while (cin.get() != '\n')
            continue;
        std::cout << "异常 - " << c_Message[0] << "以确保程序正常运行。\n";
    };
    return i_temp;
};

void timer(int seconds)
{
    int time{seconds * CLOCKS_PER_SEC};
    clock_t start{clock()};
    while (clock() - start < time);
};

/*
int main()
{
    using namespace std;

    //使用 if 指令选取执行对象
    cout << "兄弟，输个数字：";
    int* i_StartCho{new int};
    (cin >> *i_StartCho).get();
        //函数调用
        if(*i_StartCho == 0)
        {
            for_Test();
        }
        else
        {
            str_Test();
        };
    delete i_StartCho;

    return 0;
};
//0.0.3 for Release
*/